package com.utils;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @description: AUTO GENERATION
 * @date 2022/5/30 16:17
 */
public class JdbcUtil {

    private static ResourceBundle bundle = ResourceBundle.getBundle("jdbc");

    static {
//        读取配置文件
        String driver = bundle.getString("jdbc.driver");
        try {
            //MySQL驱动加载
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //    获取connect对象
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(bundle.getString("jdbc.url"), bundle.getString("jdbc.username"), bundle.getString("jdbc.password"));
    }


    // 释放资源
    public static void close(Connection conn, Statement stmt, ResultSet rs, PreparedStatement preparedStatement) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Connection connection = JdbcUtil.getConnection();
            System.out.println(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
